package com.twd.allcomponent.login.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twd.allcomponent.common.RespBean;
import com.twd.allcomponent.common.ResultVO;
import com.twd.allcomponent.login.bean.User;
import com.twd.allcomponent.login.mapper.UserMapper;
import com.twd.allcomponent.redis.utils.RedisUtils;
import com.twd.allcomponent.shiro.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService  {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtils redisUtils;
    @Override
    public ResultVO<String> login(User req) {
        redisUtils.set("qqqq","Wq");
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("phone", req.getPhone()));

        RespBean res = new RespBean();

        //判断是否已经注册的
        if (user == null) {
            //未注册
            return ResultVO.error(10000,"用户未注册");
        }

        if (req.getPassword().equals(user.getPassword())) {

            //登录成功，生成token，UUID生成token，存储到redis中并设置过期时间
            User loginUser = User.builder().build();
            BeanUtils.copyProperties(user, loginUser);
            Map<String,Object> map=new HashMap<>();
            map.put("sub",user.getUsername());
            map.put("phone",user.getPhone());
            String s = JwtUtil.generateToken(map);
//            Map<String, Object> stringObjectMap = JWTUtil.geneJsonWebToken(loginUser);
            return ResultVO.success(s);
        }
        return ResultVO.error(10000,"密码错误");
    }
}
